package com.rr.jvm.demo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhuangcanrun
 * @Date 2022/01/06/23:19
 * @description
 **/

public class Demo1GcTest {
    private static final int _512KB = 512 * 1024;
    private static final int _1MB = 1024 * 1024;
    private static final int _6MB = 6 * 1024 * 1024;
    private static final int _7MB = 7 * 1024 * 1024;
    private static final int _8MB = 8 * 1024 * 1024;

    //启动参数：-Xms20M -Xmx20M -Xmn10M -XX:+UseSerialGC -XX:+PrintGCDetails -verbose:gc
    public static void main(String[] args) {
        func4();
    }

    //空间不足触发gc
    private static void func1() {
        List<byte[]> list = new ArrayList<>();
        list.add(new byte[_7MB]);
    }

    //对象过大，新生代放不下，不触发gc，直接放老年代
    private static void func2(){
        List<byte[]> list = new ArrayList<>();
        list.add(new byte[_8MB]);
    }

    //堆放不下，minor gc,full gc都救不回来，堆内存溢出
    private static void func3(){
        List<byte[]> list = new ArrayList<>();
        list.add(new byte[_8MB]);
        list.add(new byte[_8MB]);
    }

    //一个线程内出现内存溢出，主线程不会停
    private static void func4(){
        new Thread(() -> {
            List<byte[]> list = new ArrayList<>();
            list.add(new byte[_8MB]);
            list.add(new byte[_8MB]);
        }).start();
        System.out.println("睡一秒结束");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
